package com.lintf.devdemo.controller;

import com.lintf.devdemo.common.Constant;
import com.lintf.devdemo.common.Result;
import com.lintf.devdemo.entity.dto.UserInfoDeleteDto;
import com.lintf.devdemo.entity.dto.UserInfoInsertDto;
import com.lintf.devdemo.entity.dto.UserInfoSelectDto;
import com.lintf.devdemo.entity.dto.UserInfoUpdateDto;
import com.lintf.devdemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @ system:Window
 * @ author:lin tingfeng
 * @ data:2022/09/29-11:27
 * @ package_name:com.lintf.devdemo.system.controller
 * @ project_name:DevDemo
 * @ desc:用户控制层
 */
@Api(value = "用户信息接口API")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取用户信息列表")
    @GetMapping("/getUserInfoList")
    public Result getUserInfoList() {
        return Result.success(userService.getUserInfoList());
    }

    @ApiOperation(value = "查询用户信息（条件）")
    @GetMapping("/selectUserInfoByCondition")
    public Result selectUserInfoByCondition(UserInfoSelectDto userInfoSelectDto) {
        return Result.success(userService.selectUserInfoByCondition(userInfoSelectDto));
    }

    @ApiOperation(value = "新增用户信息")
    @PostMapping("/insertUserInfo")
    public Result insertUserInfo(@RequestBody UserInfoInsertDto userInfoInsertDto) {
        if (userService.insertUserInfo(userInfoInsertDto) != 0)
            return Result.success(null);
        return Result.failed("新增用户信息已存在！");
    }

    @ApiOperation(value = "更新用户信息")
    @PutMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfoUpdateDto userInfoUpdateDto) {
        if (userService.updateUserInfo(userInfoUpdateDto) != 0)
            return Result.success(null);
        return Result.failed("更新用户信息失败！");
    }

    @ApiOperation(value = "删除用户信息")
    @DeleteMapping("/deleteUserInfo")
    public Result deleteUserInfo(@RequestBody UserInfoDeleteDto userInfoDeleteDto) {
        Integer deleteUserInfo = userService.deleteUserInfo(userInfoDeleteDto);
        if (deleteUserInfo == 1) {
            return Result.success(null, Constant.DELETE_USER_INFO_SUCCESS);
        } else if (deleteUserInfo == 2) {
            return Result.validateFailed(Constant.DELETE_USER_INFO_MISMATCH);
        }
        return Result.failed(Constant.DELETE_USER_INFO_NON_EXISTENT);
    }

    @ApiOperation(value = "删除用户信息通过用户id")
    @DeleteMapping("/deleteUserInfoById/{userInfoId}")
    public Result deleteUserInfoById(@PathVariable Integer userInfoId) {
        Integer deleteUserInfoById = userService.deleteUserInfoById(userInfoId);
        if (deleteUserInfoById == 1) {
            return Result.success(null);
        }
        return Result.failed();
    }
}